package com.neu.neumall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.neu.neumall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000121627828";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCY7WWIl/IhCKSm/NLq8S6GdLOOI+pXvuAhykTAu+CtWIYxip6PyONVnQ4XHsAeOixG8oMEP7epKuVB8FoWyHhkZ9V++lcOkQEhM0zsOggSDWDWDkvTsjeeaNCJ0KKjE/C9k16jqJgXBzE1SrHDHeP+S0Rf5n0BIevUcXxHlzstvvQuuXzskZQeAb9kOtou0mUH78SZupvoXJvZ8p2mGGbwSCa2GFLnNsTQU/3u5NMNDWidNmXgxQshLEc7QVHYSJxXb1HnW9v0nOcCMqszs4bnueXz5gZxcfIQDgOWiieMnmcwbGjARryD+Hn8//Pfc2bmaoCxYANWaMkiwFbK2jhPAgMBAAECggEBAIIVzry2o9y9wXHatn/r0SvGscZDfuBNGdMkf/+C0cgi+WPDCsY8mEUHaCj6m2obCfb/67r9iHl6Z7IDhl7DAMZIHS/6O3vElTVssg5v/tV8U9dCKfZWL0Gs31ecBfy6xZo9dc+5ZkaQc6dldnkHQB3cYLlDEyqXdEgukQ1E864g4fh2aZloZ2S94f0P0EYPX/uAIfUHkraGFs2bqTe7qlaSCOpDjPMvYkSWRPXIbga7Q3maCwT3Axbpsm4VLUVso7SWTbu5N3rgntzVUFMegDO4PZGwmdnl4uoLUjPK6mIbYSraAWB13D4muNLHFmLOwvOAFpAzxkVCqoFXwNDXi2ECgYEA30eaCcXMrn1yiAbESDA35upxg+TQFcwVPOKgDqQ8MYYDw7jq60z7Ckg7XEfkfLMrBaASTYrXcuVbyND7d3flgsjuEdy7WMEuvirNVp83VCycyJFCs14USer2bvkRUZB0vRli/yPQvt7OuQlckAaoZAwsH2n0hkWCRNu1iwec/BsCgYEAr1aBxCgjNwMeRmUwHxd+hs8BLQKuRtvIpDdcl9hhXq22zj2TIeHGzqna1UkdO0xgrW6PCGoyjJ2yJKGsdjOrcpnUNZ4Juu4bjInJ5FQzhIPcEQO3+QEJERVulyK6u0qDnh826PKhZFxt2ZhdwAwgmYST95YGmRwZ0hX8/cudD90CgYEAnXb2uIupxSkte6ciCDMRSaXvdnAJHEY4eB7veEP1O4vi3cZszpMUvHSigHsnNZZ58dao8czIEvyUFXTk06VX1Dt2QlykN3zORUkUVVGmCJQJ4h0nqC3iKxssvqiiwR4aZj4gJgWRMvsZWT+z6tL3MM+w0lq8G3Gr5Hn6d1lgKh8CgYB/yQkr/6CikDg5T7fBgSrqbuVJe4GuDLHu1JY76CwWh34KUx/u+pAcv/fHp9CRDfoLi97cYCFVD/zr65Q4VTHnhKr/JJ2syan0Q9uK977QHhV9pPsok5ObCsadiWcT5puD2vX20hXDggVhJ0V0GKKNn+0C7SkD31IGJZZLFWDzVQKBgA07gdpbq+4XXVuod5BWA9oPfhFarghqQQ1Sb/KZqlthpuBmaeHRcfPGvMqIf9Tbq7IYP0Q9uHNx9Ru9HchXsTHeWvptOx+mI+PMeFeknTptN6diJi39B6eAjLVVcXr0F14Tcv4ENahjdQWK+ke+BJDsfTE82spwx73HDlCcj7hb";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhA7W7KuQeFP4Mul4OeXL4PmVV9DdyNuy6Ac+L6ul4GJaEdfuY/n3yeUgtcdIRE3WoGw5mR9THOxsoRIUK5xx6u1y5UIZde22nqjj9jY53FBO+dw/kM/Svj4gUVt8jqkY+bI/Cfs/llO+Ubj8YcJ6c57aPnpKFKlb+47ciiJid0r/ODiWQb+IbXZcpJviEJkQzgFIPJwfTOJit4OEVuOW0oB7zgFrOYIfz+/2dnTHsFsyczbIkEVOF/PVqRBgeFTQBm80MYehYo7QKxfdP3+WhYbjHO8ZW/OdgR5tZn1qiZsMtlAlO+XXEoCu144RR6XjEYDitqtp2ARO6Ku2o5V+NQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url = "http://neumall.natapp1.cc/payed/notify";
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    public String return_url = "http://member.neumall.com/memberOrder.html";
    //同步通知，支付成功，一般跳转到成功页

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "10m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
